package Offer052;

/**
 * 展开二叉搜索树
 *
 * @author 23737
 * @time 2021.12.21
 */
public class Test {
    public static void main(String[] args) {

    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

class Solution {
    TreeNode pre = new TreeNode(), node = pre;
    public TreeNode increasingBST(TreeNode root) {
        dfs(root);
        return pre.right;
    }

    //    根据二叉树的左小右大的性质
    public void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        dfs(root.left);
        // 添加节点
        node.right = new TreeNode(root.val);
        // 继续向右节点添加
        node = node.right;
        dfs(root.right);
    }
}
